package chainOfResponsibility;

import gameBasics.GameObject;
import object.Wall;
import tank.bullet.Bullet;
import tank.explosion.Explosion;

public class BulletWallCollider implements Collider{

    /**
     * 炮弹与墙碰撞
     * @param o1 炮弹对象
     * @param o2 墙对象
     */
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if(o1 instanceof Bullet bullet && o2 instanceof Wall wall){
            return !collideWith(bullet, wall);
        } else if (o1 instanceof Wall && o2 instanceof Bullet) {
            return !collide(o2, o1);
        }

        return true;
    }

    /**
     * 炮弹与墙的碰撞检测
     * @param bullet 炮弹对象
     * @param wall 墙对象
     */
    public boolean collideWith(Bullet bullet, Wall wall) {
        if(bullet.getRectangle().intersects(wall.getRectangle())){
            wall.die();     //墙消亡
            bullet.die();   //炮弹消亡
            new Explosion(wall.x + wall.WIDTH / 2 - Explosion.WIDTH / 2, wall.y + wall.HEIGHT / 2 - Explosion.HEIGHT / 2);

            return true;    //相撞
        }

        return false;
    }

}
